package com.xhu.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhu.common.vo.SaleperformVo;
import com.xhu.exceptions.SaleperformException;
import com.xhu.mapper.DeptMapper;
import com.xhu.mapper.SaleperformMapper;
import com.xhu.pojo.Dept;
import com.xhu.pojo.Saleperform;
import com.xhu.service.ISaleperformService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class SaleperformServiceImpl extends ServiceImpl<SaleperformMapper, Saleperform>
        implements ISaleperformService {

    @Autowired
    SaleperformMapper mapper;

    @Autowired
    DeptMapper deptMapper;

    @Override
    public IPage<SaleperformVo> getAll(int current, int size) {

        return mapper.getAll(new Page<>(current, size));

    }

    @Override
    public IPage<SaleperformVo> getAllByName(int current, int size, String name) {
        return mapper.getByName(new Page<>(current, size), name);
    }

    @Override
    public List<Dept> getAllDepts() {
        return deptMapper.selectList(null);
    }

    @Override
    public void deleteById(String id) {
        if (!StringUtils.hasText(id)) {
            throw new SaleperformException("数据为空");
        }

        boolean flag = this.removeById(id);

        if (!flag) {
            throw new SaleperformException("删除失败");
        }
    }

    @Override
    public void insertOne(SaleperformVo vo) {

        if (vo == null) {
            throw new SaleperformException("数据为空");
        }

        mapper.insertOne(vo);
    }

    @Override
    public void update(SaleperformVo vo) {
        if (vo == null) {
            throw new SaleperformException("数据为空");
        }

        Integer res = mapper.update(vo);

        if (res != 1) {
            throw new SaleperformException("更新失败");
        }

    }


}

